/**
 * @description: 我的积分
 *
 * @author mashaohua
 * @date 2018年7月10日 下午10:00:52
 * @version 1.0.0.1
 */

var integralhtm = {

//		全局变量
        token : null,
        cid : null,
        pageIndex : null,
		init : function(accessToken,cid){
			integralhtm.token = accessToken;
			integralhtm.cid = cid;
			return integralhtm;
		},
		/*历史积分，剩余积分  */
		 initIntegralRecord : function(){
			 var url = integralhtm.getRootPath()+ "/aem/person/getIntegralCount";
			$.post(
					url,
					{
						"staffToken":integralhtm.token
					},
					function(data){
						if(data != null){
							$("#historyIntegarl").text(data.historyScore);
							$("#Integral").text(data.surplusScore);
						}
					},
					"json"
				);
		},
		/*积分明细列表  */
		 integralList : function(pageIndex){
			 integralhtm.pageIndex = pageIndex;
			 var url = integralhtm.getRootPath()+ "/aem/person/getIntegralList";
			$.post(
					url,
					{
						"staffToken":integralhtm.token,
						"index":integralhtm.pageIndex,
			        	"size":10,
					},
					function(data){
						if(data.status == "200"){
							var list = data.data.list;
							var arrayObj = new Array();
							var html_ = '';
							for(var i = 0 ; i < list.length ; i ++){
								var date = formatDateTimeYMDHMSDAY(list[i].createTime);
								var dataTime = date.substring(0,11);
								if(arrayObj.includes(dataTime)){
									if(list[i].score >0){
										if(list[i].scoreType == 0){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">邀请会员注册</span>';
										}else if(list[i].scoreType == 1){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">邀请粉丝关注</span>';
										}else if(list[i].scoreType == 2){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">点击活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">奖励</span>';
										}else if(list[i].scoreType == 3){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">分享活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">奖励</span>';
										}else if(list[i].scoreType == 4){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">'+ list[i].operatorName +'给我添加积分</span>';
										}else if(list[i].scoreType == 5){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">分享的活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">被'+list[i].clickNickName+'点击</span>';
										}else {
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">分享的活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">被'+list[i].clickNickName+'分享</span>';
										}
										html_ += '</div><div class="in-time">' + date +'</div>'
												+'<div class="add-code">'+'+' + list[i].score +'</div></div>';
									}else{
										html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">'+ list[i].operatorName +'给我减少积分</span>'
												+'</div><div class="in-time">' + date +'</div>'
												+'<div class="subtract-code">' + list[i].score +'</div></div>';
										}
								}else{
									arrayObj. push(dataTime)
									if(list[i].score >0){
										html_ += '<div class="integral-tt" style="background-color:#C0C0C0">' + dataTime +'</div>';
										if(list[i].scoreType == 0){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">邀请会员注册</span>';
										}else if(list[i].scoreType == 1){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">邀请粉丝关注</span>';
										}else if(list[i].scoreType == 2){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">点击活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">奖励</span>';
										}else if(list[i].scoreType == 3){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">分享活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">奖励</span>';
										}else if(list[i].scoreType == 4){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">'+ list[i].operatorName +'给我添加积分</span>';
										}else if(list[i].scoreType == 5){
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">分享的活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">被'+list[i].clickNickName+'点击</span>';
										}else {
											html_ += '<div class="integral-item"><div class="in-tt"><span style="color:black">分享的活动</span><span>【'+ list[i].aemName +'】</span><span style="color:black">被'+list[i].clickNickName+'分享</span>';
										}
										html_ += '</div><div class="in-time">' + date +'</div>'
												+'<div class="add-code">'+'+' + list[i].score +'</div></div>';
									}else{
										html_ += '<div class="integral-tt">' + dataTime +'</div>'
										+'<div class="integral-item"><div class="in-tt"><span style="color:black">'+ list[i].operatorName +'给我减少积分</span>'
										+'</div><div class="in-time">' + date +'</div>'
										+'<div class="subtract-code">' + list[i].score +'</div></div>';
										}
									}
								}
									$("#listInt").append(html_);

						}else if(data.status == "300"){
							if(pageIndex == 1){
								html_='';
								var href = integralhtm.getRootPath() +"/aem/client/aemTaskList?accessToken="+integralhtm.token+"&cid="+integralhtm.cid;
								html_ +='<div><span style="color:black">还没有积分记录，</span><a href="'+href+'">分享任务</a>、<span>引导粉丝关注可赢取积分>></span></div>';
								$("#listInt").append(html_);
							}
							loadValid = false ;
						}else{
							html_='';
							var href = integralhtm.getRootPath() +"/aem/client/aemTaskList?accessToken="+integralhtm.token+"&cid="+integralhtm.cid;
							html_ +='<div><span style="color:black">还没有积分记录，</span><a href="'+href+'">分享任务</a>、<span>引导粉丝关注可赢取积分>></span></div>';
							$("#listInt").append(html_);
						}
					},
					"json"
				);
		},

		//获取路劲
	    getRootPath:function(){
		     return $("#basePath").val();
	    },

		//获取路径里的参数

		 getUrlParam : function(key){
			var reg = new RegExp("(^|&)"+ key +"=([^&]*)(&|$)");
		    var r = window.location.search.substr(1).match(reg);
		    if(r!=null)return  unescape(r[2]); return null;
		}



}
$('.rule').click(function(){
	window.location.href=integralhtm.getRootPath() +"/aem/person/recordIntegral?cid="+integralhtm.cid;
});


var loadValid = true;


//监听是否滑到底部
window.addEventListener('scroll', throttle(loadData, 500, {leading:false}), false);

//加载更多数据
function loadData(){
	if($(window).scrollTop() + $(window).height() >= $(document).height() && loadValid){
		integralhtm.pageIndex++;
		integralhtm.integralList(integralhtm.pageIndex);
	}
}
//频率控制函数
function throttle(fn,delay,options) {
     var wait=false;
     if (!options) options = {};
     return function(){
         var that = this,args=arguments;
         if(!wait){
             if (!(options.leading === false)){
                 fn.apply(that,args);
             }
             wait=true;
             setTimeout(function () {
                 if (!(options.trailing === false)){
                     fn.apply(that,args);
                 }
                 wait=false;
             },delay);
         }
     }
 }
